#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <fstream>
#include <ostream>
using std::cout;
using std::endl;
using std::map;
using std::string;
using std::ifstream;
using std::ofstream;
using std::cerr;
using std::istringstream;

class Dictonary{
public:
    void readFile(const string & filename){
        ifstream ifs(filename);
        if(!ifs){
            cerr << "open file failed" << endl;
            return;
        }

        string line;
        while(getline(ifs,line)){
            istringstream iss(line);
            string word;
            while(iss >> word){
                string newword = dealword(word);

                if(word != string()){
                    ++_dict[newword];
                }
            }
        }
        ifs.close();
    }

    string dealword(const string &word){
        for(size_t idx = 0;idx != word.size(); ++idx){
            if(!isalpha(word[idx])){
                return string();
            }
        }
        return word;
    }

    void store(const string&filename){
        ofstream ofs(filename);
        if(!ofs){
            cerr << "ofstream open file failed" << endl;
            return;
        }

        auto it = _dict.begin();
        for(;it != _dict.end(); ++it){
            ofs << it->first << " " << it->second << endl;
        }
        
        ofs.close();
    }
private:
    map<string,int> _dict;
};

void test0(){
    Dictonary mapdict;
    mapdict.readFile("The_Holy_Bible.txt");
    mapdict.store("mapDict.dat");

}

int main(int argc, char * argv[])
{
    test0();
    return 0;
}


